package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-23
 */
@Service
@RequiredArgsConstructor
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {
    private final ShoppingCartMapper shoppingCartMapper;
    private final DishMapper dishMapper;
    private final SetmealMapper setmealMapper;

    @Override
    public Result<String> add(ShoppingCartDTO shoppingCartDTO) {
        QueryWrapper<ShoppingCart> wrapper=new QueryWrapper<>();
        Long dishId=shoppingCartDTO.getDishId();
        Long setmealId=shoppingCartDTO.getSetmealId();
        Long userId= BaseContext.getCurrentId();
        wrapper.lambda()
                .eq(ShoppingCart::getUserId,userId)
                .eq(dishId!=null,ShoppingCart::getDishId,dishId)
                .eq(setmealId!=null,ShoppingCart::getSetmealId,setmealId);
        //先查询购物车里面是否有该菜品或者套餐
        ShoppingCart cart = shoppingCartMapper.selectOne(wrapper);
        //如果有，数量加一
        if(BeanUtil.isNotEmpty(cart)){
            cart.setNumber(cart.getNumber()+1);
            UpdateWrapper<ShoppingCart> updateWrapper=new UpdateWrapper<>();
            updateWrapper.lambda().eq(ShoppingCart::getId,cart.getId());
            shoppingCartMapper.update(cart,wrapper);
        }
        //如果没有，添加
        //添加的时候还要看是菜品还是套餐，因为要往里面补充相关的数据
        else{
            ShoppingCart shoppingCart=new ShoppingCart();
            BeanUtil.copyProperties(shoppingCartDTO,shoppingCart);
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            if(dishId!=null){
                QueryWrapper<Dish> dishQueryWrapper=new QueryWrapper<>();
                dishQueryWrapper.lambda()
                        .eq(Dish::getId,dishId);
                Dish dish = dishMapper.selectOne(dishQueryWrapper);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
            }else{
                QueryWrapper<Setmeal> setmealQueryWrapper=new QueryWrapper<>();
                setmealQueryWrapper.lambda()
                        .eq(Setmeal::getId,setmealId);
                Setmeal setmeal = setmealMapper.selectOne(setmealQueryWrapper);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCartMapper.insert(shoppingCart);
        }
        return null;
    }
}
